# To change this template, choose Tools | Templates
# and open the template in the editor.
require 'win32ole'
require 'util'

class Powerpoint
  @@powerpoint = nil
  def initialize(file_path)
    @file_path = file_path
    if (!@@powerpoint)
      @@powerpoint=WIN32OLE.new("PowerPoint.application")
      @@powerpoint['Visible']=TRUE
    end
  end
  def open(mode)
    @presentation = @@powerpoint.Presentations.open @file_path
  end
  def presentation
    @presentation
  end
  def write(text)
    @presentation.slides.add(@presentation.slides.count+1,2).shapes(2).textframe.textrange.text = text
    @presentation.save
  end
  def find(text)
    @presentation.slides.each { |slide|
      slide.shapes.each { |shape|
        if shape.name.index("Text Box") != nil || shape.name.index("Rectangle") != nil
          return true if shape.textframe.textrange.find(text) != nil
        elsif text == "Picture"
          return true if shape.name.index("Picture")
        end
      }
    }
  end
  def match(find_or_get, conditions, results)
    if methods.include?(find_or_get)
      command = method(find_or_get).call
    else
      command = find(find_or_get)
    end
    puts "find? #{command}"
    command = command
    Util.command_send(command, conditions, results)
  end
  def check_answer(answer_lines)
    answer_count = 0
    answer_lines.each { |answer|
      if (match(answer[0], answer[1], answer[2]))
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} match"
        answer_count += 1
      else
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} not match"
      end
    }
    return answer_count
  end
  def close
    @presentation.close
  end
end
